package util;

import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;

/**
 * 数据库连接配置帮助类
 *
 * @author SoChishun
 * @version 1.0 2016-5-13
 */
public class ConnectionConfUtil {

    /**
     * 读取数据库连接配置
     *
     * @return
     * @version 1.0 2016-5-16
     * @see http://www.cnblogs.com/manongxiaojiang/archive/2012/10/13/2722068.html
     */
    public static boolean Read() throws IOException, SQLException {
        String separator = System.getProperty("file.separator");
        String con_path = "./Data/connection.xml";
        ConnectionInfo conninfo = null;
        String content = FileUtil.readFile(con_path);
        if (null != content && content.trim().length()>0) {            
            try {
                conninfo=JAXBUtil.deserialize(ConnectionInfo.class, con_path);
            } catch (JAXBException ex) {
                Logger.getLogger(ConnectionConfUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (null == conninfo) {
            conninfo = new ConnectionInfo();
            conninfo.host = "117.25.177.101";
            conninfo.port=3306;
            conninfo.charset = "UTF-8";
            conninfo.password = "root!@#xcall098)(*Admin%^&"; // root!@#xcall098)(*Admin%^&
            conninfo.user = "xcall";
            conninfo.database = "db_xcrm_ua";
            conninfo.driver = "com.mysql.jdbc.Driver";
            conninfo.useSSL="false";
            try {
                JAXBUtil.serialize(conninfo, "./Data/connection.xml");
            } catch (JAXBException ex) {
                Logger.getLogger(ConnectionConfUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            DbUtil.setConnInfo(conninfo);
            return false;
        }
        DbUtil.setConnInfo(conninfo);
        return true;
    }
}
